package cn.edu.lcu.cs.javateaching.stream;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;

/**
 * 数学序列生成器显示，使用Stream的功能
 * 
 * @author Ling
 *
 */
public class SequenceGenerator {
	public static void main(String[] args) {

		System.out.println(Arrays.toString(arithmeticSequence(2, 2, 5)));
		System.out.println(Arrays.toString(arithmeticSequence(0, 3, 15)));
		
		
		System.out.println(Arrays.toString(geometricSequence(1, 2, 15)));
	}
	
	/**
	 * 等比序列
	 * 
	 * @param seed 种子
	 * @param factor 比例因子
	 * @param length 元素个数
	 * @return
	 */
	public static double[] geometricSequence(double seed, double factor, int length) {
		return DoubleStream.iterate(seed, n -> n * factor).limit(length).toArray();
	}

	/**
	 * 等差序列
	 * 
	 * @param begin 起始值
	 * @param step 步长
	 * @param length 元素个数
	 * @return
	 */
	public static int[] arithmeticSequence(int begin, int step, int length) {
		return IntStream.iterate(begin, n -> n + step).limit(length).toArray();
	}
}
